package com.xueyi.system.weather;

import com.alibaba.fastjson.JSONObject;
import com.xueyi.common.core.utils.HttpClientUtil;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.system.BasePlantBreed.domain.dto.BasePlantBreedDto;
import com.xueyi.system.BasePlantBreed.domain.query.BasePlantBreedQuery;
import com.xueyi.system.BasePlantBreed.service.IBasePlantBreedService;
import com.xueyi.system.area.domain.dto.BaseAreaDto;
import com.xueyi.system.area.domain.query.BaseAreaQuery;
import com.xueyi.system.area.service.IBaseAreaService;
import com.xueyi.system.parcel.domain.dto.BaseParcelDto;
import com.xueyi.system.parcel.service.IBaseParcelService;
import com.xueyi.system.plantManager.domain.dto.BasePlantManagerDto;
import com.xueyi.system.plantManager.domain.query.BasePlantManagerQuery;
import com.xueyi.system.plantManager.service.IBasePlantManagerService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zlf
 * @version 1.0
 * @data 2023/3/22 17:18
 */

@RestController
@RequestMapping("/getWeather")
public class weatherController {

    @Autowired
    private IBaseAreaService baseAreaService;

    @Autowired
    private IBaseParcelService baseParcelService;

    @Autowired
    private IBasePlantManagerService basePlantManagerService;

    @Autowired
    private IBasePlantBreedService basePlantBreedService;

    @GetMapping("/getSky")
    public String getWeather(String params){
        try {
            String location = "32.47796%2C117.16766";

            String url = "https://apis.map.qq.com/ws/geocoder/v1/?key=BKXBZ-Q6LK2-A5YUT-CMI7V-75X5Q-D3BLF&location=" + params;
            String data = HttpClientUtil.doGet(url,"UTF-8");

            JSONObject json = JSONObject.parseObject(data);

            JSONObject result = (JSONObject) json.get("result");
            JSONObject ad_info = (JSONObject) result.get("ad_info");
            String adcode = (String) ad_info.get("adcode");
            System.out.println(result);
            System.out.println(ad_info);
            System.out.println(adcode);

            String keyUrl = "https://restapi.amap.com/v3/weather/weatherInfo?key=3c6567ad570fbd3d2502bb624c6488a2&city=" + adcode;

            String keyData = HttpClientUtil.doGet(keyUrl,"UTF-8");
            System.out.println("天气" + keyData);

            return keyData;
        }catch (Exception e){
            return "接口异常" + e;
        }
    }


    /**
     * 首页接口
     */

    @GetMapping("/getList")
    public AjaxResult getList(){

        BasePlantManagerQuery managerQuery = new BasePlantManagerQuery();
        managerQuery.setPlantStatus("1");
        List<BasePlantManagerDto> dtoList = basePlantManagerService.selectList(managerQuery);
        BigDecimal plantSum = new BigDecimal("0");
        for (BasePlantManagerDto managerDto : dtoList) {
            BaseParcelDto parcelDto = baseParcelService.selectById(managerDto.getPacelId());
            plantSum = plantSum.add(parcelDto.getArea());
        }


        BaseAreaQuery areaQuery = new BaseAreaQuery();
        BigDecimal areaSum = new BigDecimal("0");
        List<BaseAreaDto> areaDtoList = baseAreaService.selectList(areaQuery);
        for (BaseAreaDto areaDto : areaDtoList) {
            areaSum = areaSum.add(areaDto.getArea());
        }

        BasePlantBreedQuery breedQuery = new BasePlantBreedQuery();
        breedQuery.setStatus("0");
        List<BasePlantBreedDto> plantBreedDtoList = basePlantBreedService.selectList(breedQuery);

        managerQuery.setPlantStatus("1");
        List<BasePlantManagerDto> listScope = basePlantManagerService.selectListScope(managerQuery);
        List<BasePlantManagerDto> plantManagerDtoList = listScope.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                new TreeSet<BasePlantManagerDto>(Comparator.comparing(BasePlantManagerDto::getPlantVariety))), ArrayList::new));

        List list = new ArrayList<>();
        Map map = new HashMap<>();
        Map map1 = new HashMap<>();
        Map map2 = new HashMap<>();
        Map map3 = new HashMap<>();
        map.put("plantSum",plantSum);
        map.put("plantSumName","基地面积");

        map1.put("areaSum",areaSum);
        map1.put("areaSumName","种植面积");

        map2.put("plantBreedDtoList",plantBreedDtoList.size());
        map2.put("plantBreedDtoListName","种植品种");
        map3.put("plantManagerDtoList",plantManagerDtoList.size());
        map3.put("plantManagerDtoListName","当前种植");
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);

        return AjaxResult.success(list);
    }


}
